package peer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XStreamTools {
	
	public static ConfigFile getConfig(){
		ConfigFile config=new ConfigFile();
		XStream xs = new XStream(new DomDriver());
		File cf=new File("config.txt");
		if(cf.exists()){
			FileInputStream fis;
			try {
				fis = new FileInputStream(cf);
				config=(ConfigFile) xs.fromXML(new InputStreamReader(fis));
			} catch (FileNotFoundException e2) {
				e2.printStackTrace();
			}
		}
		return config;
	}
	
	public static void saveConfig(ConfigFile config){
		XStream xs = new XStream(new DomDriver());
		FileOutputStream fs;
		try {
			fs = new FileOutputStream("config.txt");
			xs.toXML(config, fs);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
